package com.eatme.web.sys.service.impl;

import com.eatme.web.common.vo.AppResponse;
import com.eatme.web.sys.entity.SysResource;
import com.eatme.web.sys.repository.SysResourceRepository;
import com.eatme.web.sys.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysResourceServiceImpl implements SysResourceService {

//    @Autowired
//    SysResourceMapper sysResourceMapper;

    @Autowired
    SysResourceRepository sysResourceRepository;


//    @Override
//    public Set<AuthResource> findAllResource() {
//        return sysResourceMapper.findAllResource();
//    }

//    @Override
//    public List<SysResourceDto> findMenu() {
//        return sysResourceMapper.findMenu();
//    }

//    @Override
//    public List<SysResourceDto> findRoleMenu(Set<AuthRole> roles) {
//        return sysResourceMapper.findRoleMenu(roles);
//    }

    @Override
    public SysResource getDetail(Integer id) {
        return sysResourceRepository.findById(id).get();
    }

    @Override
    public List<SysResource>  findList(Integer parentId) {
        // return sysResourceMapper.findList();
        return sysResourceRepository.findByParentId(parentId);
    }

    @Override
    public AppResponse add(SysResource sysResource) {
        sysResource.setSeq(0);
        sysResource = sysResourceRepository.save(sysResource);
        return AppResponse.ok();
    }
    @Transactional
    @Override
    public AppResponse delete(Integer id) {
         SysResource sysResource = sysResourceRepository.getReferenceById(id);
         if (sysResource.getChildren().isEmpty()){
             sysResourceRepository.updateSysResourceStatusById(id);
         }else {
             return AppResponse.error("当前资源存在子资源无法删除");
         }

        return AppResponse.ok();
    }
}
